// License Notification
//======================================================
// Copyright (c) 2010 Channel Intelligence Inc.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// ======================================================




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CIP4.Common;

namespace CIP4.RulesEngine
{
    public class ObjectDataPacketType: IDataPacketType
    {
        private object _Value;

        private Dictionary<string, PathEvaluatorType> _KnownPaths = new Dictionary<string, PathEvaluatorType>();

        private string _RootName;

        public ObjectDataPacketType()
        {
        }

        #region IDataPacketType Members

        public void Bind(string rootName, object value)
        {
            _RootName = rootName;
            _Value = value;
        }

        public object GetNamedValue(string name)
        {
            if (!name.StartsWith(_RootName))
                return null;
            if (!_KnownPaths.ContainsKey(name))
                _KnownPaths.Add(name, new PathEvaluatorType(name));
            return _KnownPaths[name].GetValues(_Value).FirstOrDefault();
        }

        public GetValueDelegate GetValue { get; set; }

        public LoggerDelegate Logger {get;set;}

        IRulesEngineOperationsType _Operations;
        public IRulesEngineOperationsType Operations 
        {
            get
            {
                if (_Operations == null)
                    _Operations = new RulesEngineOperationsType(this);
                return _Operations;
            }
            set
            {
                _Operations = value;
            }
        }

        public IPacketStatsType PacketStats { get; set; }

        Dictionary<string, object> _Parameters = new Dictionary<string, object>();
        public Dictionary<string, object> Parameters { get { return _Parameters; } set { _Parameters = value; } }

        public void Reset()
        {
            _Parameters = new Dictionary<string, object>();
        }

        public SetValueDelegate SetValue
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion
    }
}
